#!/bin/bash
# Author    : some-guy (Muhammed U)
# Copyright : (C) 2008 some-guy
# E-mail    : muhammedu (at) gmail (dot) com

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
CONFIGURE_ARGS="--prefix=/usr --build=i486-Slackware-linux"
BUILD_FLAGS="-O3 -march=i486 -mtune=i686"
BUILD_TMP="/tmp"
BUILD_ROOT="$BUILD_TMP/sgfakeroot"

# == import libslaxget
if [ -f libslaxget ]; then
	. libslaxget
elif [ -f /usr/lib/slax-get/libslaxget ]; then
	. /usr/lib/slax-get/libslaxget
else
	echo "libslaxget not found!"
	exit 1
fi

# == find out how to build
# no args, prints type
function slax_config {
# == are we using autogen.sh, configure, or python?
if [ -f configure ]; then 
	echo "configure"
elif [ -f autogen.sh ]; then 
	echo "autogen.sh"
elif [ -f setup.py ]; then
	echo "setup.py"
else 
	echo "none"
fi
}

# == build archive
# $1 == containing dir
# $2 == type
# $3 == configure/autogen args
# $4 == additional commands
function slax_build {
OLDDIR=`pwd`
cd $1
PACKAGE_DIR=`pwd`
# ==  make sure we can build
if [ $2 == "none" ]; then
	echo "FAILED: Don't know how to build"
	exit 77
fi
# == build properly
if [ $2 == "autogen.sh" ] || [ $2 == "configure" ]; then

	# == configure
	slax_print "Running ./$2 $CONFIGURE_ARGS $3"; sleep 1
	export CFLAGS=$BUILD_FLAGS
	./$2 $CONFIGURE_ARGS $3
	if [ $? -ne 0 ]; then
		slax_bail "Check failed, do you have all of the deps?"
	fi

	# == make
	slax_print "Running make"; sleep 1
	make

	# == install
	slax_print "Running make install DESTSIR=$BUILD_ROOT"; sleep 1
	mkdir $BUILD_ROOT
	make install DESTDIR=$BUILD_ROOT

elif [ $2 == "setup.py" ]; then

	# == install
	mkdir $BUILD_ROOT
	slax_print "Running python $2 install --prefix=/usr --root=$BUILD_ROOT $3"; sleep 1
	python $2 install \
 		--prefix=/usr \
 		--root=$BUILD_ROOT $3
fi

cd $BUILD_ROOT

# == prep for lzm'ing
slax_print "Cleaning up"
find ./ -type d | xargs chmod -v 755 &>/dev/null
find ./usr/man/ -type l -name "*.gz" | xargs -r gunzip -f &>/dev/null
find ./usr/man/ ! -type l -name "*.gz" | xargs -r gunzip &>/dev/null
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded &>/dev/null

# == other cmd's
slax_print "Running additional commands"
$4

# == generate package metadata
slax_print "Generating Package Metadata"
PACKAGE_NAME=`basename $PACKAGE_DIR`
PACKAGE_FILES=`find $BUILD_ROOT/ | sed -e s:$BUILD_ROOT/::g -e s:$BUILD_ROOT::g`
mkdir -p var/log/packages &>/dev/null
cat << EOF >var/log/packages/$PACKAGE_NAME-1sg
PACKAGE NAME:     $PACKAGE_NAME-1sg
COMPRESSED PACKAGE SIZE:     42 K
UNCOMPRESSED PACKAGE SIZE:     130 K
PACKAGE LOCATION: $PACKAGE_NAME-1sg.tgz
PACKAGE DESCRIPTION:
$PACKAGE_NAME: $PACKAGE_NAME-1sg
$PACKAGE_NAME: 
$PACKAGE_NAME: This package was generated with slax-get
$PACKAGE_NAME: and comes with absolutely no warrenty, or
$PACKAGE_NAME: garentee of any kind, and is to be used at
$PACKAGE_NAME: YOUR OWN RISK.
$PACKAGE_NAME: 
$PACKAGE_NAME: Generated on `date +%Y%m%d`
$PACKAGE_NAME: 
$PACKAGE_NAME: http://www.slax.org/forum.php?action=view&parentID=329
$PACKAGE_NAME: 
FILE LIST:
./
install/
install/slack-desc
install/doinst.sh
install/slack-required
EOF
for singlefile in $PACKAGE_FILES; do
	echo $singlefile >>var/log/packages/$PACKAGE_NAME-1sg
done

cd $BUILD_TMP

# == lzmify
slax_print "Generating $PACKAGE_NAME-1sg.lzm"
dir2lzm $BUILD_ROOT $BUILD_TMP/$PACKAGE_NAME-1sg.lzm >/dev/null
if [ $? == 0 ]; then
	echo "Successfully built package!"
else
	echo "Failed to build package!"
	exit 3
fi

# == get rid of junk
slax_print "Removing build junk"
rm -rf $BUILD_ROOT

slax_print "Package is available at $BUILD_TMP/$PACKAGE_NAME-1sg.lzm"
cd $OLDDIR
}
